<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

/**
 * 订单－商品关联表
 * Class CreateOrderProductTable
 */
class CreateOrderProductTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('order_product', function(Blueprint $table)
        {
            $table->increments('id')->comment('主键，自增，关联表id');
            $table->unsignedInteger('order_id')->comment('外键，订单id');
            $table->unsignedInteger('product_id')->comment('外键，商品id');
            $table->unsignedInteger('count')->comment('购买的商品数');
            $table->timestamps();
        });

        Schema::table('order_product', function($table)
        {
            $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('order_product');
    }

}
